package cn.gson.crm.controller.emplo;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmploDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.EmploPo;
import cn.gson.crm.model.entity.Employee;

/**
 * 创建员工
 */
@WebServlet("/employee/add")
public class EmploAddServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private EmploDao emploDao = new EmploDao();
	private DeptDao deptDao = new DeptDao();

	/**
	 * 员工创建
	 */
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.get(request, response);
		// 获取部门
		List<Dept> deptList = deptDao.getDeptList(1);
		request.setAttribute("deptList", deptList);

		String eid = request.getParameter("eid");

		if (eid != null && !eid.isEmpty()) {
			Employee e = emploDao.emploGetById("正常", Integer.parseInt(eid));
			request.setAttribute("e", e);
		}
		getServletPathAndForward1(request, "employee/emplo_add.jsp");
		//forward("employee/emplo_add.jsp");
	}

	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String eid = request.getParameter("eid");
		String dept = request.getParameter("dept");
		System.out.println(dept);
		String userName = request.getParameter("userName");
		System.out.println(userName);
		String password = request.getParameter("password");
		String gender = request.getParameter("gender");
		String telphone = request.getParameter("telphone");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String empType = request.getParameter("empType");

		// 创建员工
		if (userName == null || userName.isEmpty()) {
			request.setAttribute("error", "用户名不能为空!");
			get(request, response);
			return;
		}else if(userName.trim()==null||userName.trim().isEmpty()){
			request.setAttribute("error", "用户名不能为空!");
			get(request, response);
			return;
		}
		if (password == null || password.isEmpty()) {
			request.setAttribute("error", "密码不能为空!");
			get(request, response);
			return;
		}else if (password.trim() == null || password.trim().isEmpty()) {
			request.setAttribute("error", "密码不能为空!");
			get(request, response);
			return;
		}

		if (empType == null || empType.isEmpty()) {
			request.setAttribute("error", "员工类型不能为空!");
			get(request, response);
			return;
		}

		Employee e = new Employee();
		e.setUserName(userName);
		e.setPassword(password);
		if (dept != null && !dept.isEmpty()) {
			e.setDeptId(Integer.parseInt(dept.toString()));
		}
		e.setGender(gender);
		e.setTelphone(telphone);
		e.setEmail(email);
		e.setAddress(address);
		if (empType != null && !empType.isEmpty()) {
			e.setEmploType(Integer.parseInt(empType.toString()));
		}
		e.setStatus("正常");

		try {
			if(emploDao.addEmplo(e)){
				 redirect("employee");
				 return;
			 }	
		} catch (SQLException e2) {
			request.setAttribute("error", "名字已存在!");
			get(request, response);
			return;
		}
		request.setAttribute("error", "保存失败!");
		get(request, response);
	}

}
